def pr(x):
    k = 0
    for i in range(2,x):
        if x % i == 0:
            k = k + 1
    if k != 0:
        return False
    return True
def vh(x,a):
    if str(x) in str(a):
        return True
    return False

a = int(input())
m = 0
if 999 < a <= 9999:
    for i in range(a+1):
        if pr(i) and vh(i,a):
            if m < i:
                m = i
print(m)
